<?php
/*
 * @Author: CYH
 * @Date: 2020-10-31 10:25:06
 * @LastEditTime: 2021-04-22 10:42:04
 * @LastEditors: Please set LastEditors
 * @Description: 物品管理model类
 * @FilePath: \application\models\Article\Article_manage.php
 */ 
    // 物品管理modle类
    class Article_manage extends CI_Model
    {
        // 构造方法
        public function __construct()
        {
            parent::__construct();
            $this->load->database();
        }
        
        /**
         * @description: 房间资产初始化登陆方法
         * @param {initPassword}
         * @return {initPassword}
         */        
        public function room_article_init_login($initPassword)
        {
            //必输值为空报错
            if ($initPassword == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入密码' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                $query = $this->db->get('dorm_inpassword');
                //判断密码
                if ($initPassword != $query->row()->initPassword) 
                {
                    //密码错误
                    $return_fa = array('res' => '40001', 'msg' => '密码错误', 'data' => '');
                    $result = json_encode($return_fa);
                } 
                else 
                {
                    //密码正确，查询成功
                    $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->row()->initPassword);
                    $result = json_encode($return_su);
                }
            }
            return $result;
        }

        /**
         * @description: 房间资产初始化表单方法
         * @param {area_number, floor_number, floor1, floor2}
         * @return {*}
         */        
        public function room_article_init_form($area_number, $floor_number, $floor1, $floor2)
        {
            //必输值为空报错
            if ($area_number == '' || $floor_number == '' || $floor1 == '' || $floor2 == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                //楼层下限大于等于上限时才进行逻辑计算
                if ($floor2 >= $floor1) 
                {
                    //循环对范围内的楼层进行初始化
                    for ($floorNow = $floor1; $floorNow <= $floor2; $floorNow++) 
                    {
                        $where = array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $floorNow);
                        $this->db->delete('dorm_fixedassets', $where);
                        $return = $this->db->affected_rows();
                        $query = $this->db->get_where('dorm_fixedassets', array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $floorNow));
                        if ($return) 
                        {
                            $return_su = array('res' => '40000', 'msg' => '房间固定资产初始化成功', 'data' => $query->result());
                            $result = json_encode($return_su);
                        } 
                        else 
                        {
                            $return_fa = array('res' => '40001', 'msg' => '房间初始化失败', 'data' => '');
                            $result = json_encode($return_fa);
                        }
                    }
                //若楼层上限大于下限则报错
                } 
                else 
                {
                    $return = array('res' => '40000', 'msg' => '上限比下限大' ,'data' => '');
                    $result = json_encode($return);
                }
            }
            return $result;
        }

        /**
         * @description: 房间初始化新增资产方法
         * @param {area_number, floor_number, floor1, floor2, dorm_property_description, dorm_property_number, totalNumber, price, compensation_price}
         * @return {*}
         */        
        public function room_article_init_add($area_number, $floor_number, $floor1, $floor2, $dorm_property_description, $dorm_property_number, $totalNumber, $price, $compensation_price)
        {
            //必输值为空报错            
            if ($area_number == '' || $floor_number == '' || $floor1 == '' || $floor2 == '' || $dorm_property_description == '' || $dorm_property_number == '' || $totalNumber == '' || $price == '' || $compensation_price == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                //楼层下限大于等于上限时才进行逻辑计算
                if ($floor2 >= $floor1) 
                {
                    for ($floorNow = $floor1; $floorNow <= $floor2; $floorNow++) 
                    {
                    //取当前区域当前宿舍楼当前楼层的房间数
                    // $where = array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $floorNow);
                    // $this->db->select('room_number');
                    // $query = $this->db->get('dorm_sslmanage');
                    $query = $this->db->get_where('dorm_roominfor' ,array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $floorNow));
                    //将取出的数据转int
                    $room_number = $query->num_rows();
                        for ($number_now = 1; $number_now <= $room_number; $number_now++) 
                        {
                            //若number_now <10则加0拼接房间号
                            if ($number_now < 10) 
                            {
                                //拼接房间号
                                $number_now1 = $floorNow .'0' .$number_now;
                            } 
                            else 
                            {
                                //若number_now不小于10则直接拼接房间号
                                $number_now1 = $floorNow .$number_now;
                            }
                            $data = array(
                              'area_number' => $area_number,                            //区域编号
                              'floor_number' => $floor_number,                          //宿舍楼编号
                              'dorm_floor' => $floorNow,                                //宿舍楼层号
                              'room_number' => $number_now1,                            //宿舍房间号
                              'dorm_property_number' => $dorm_property_number,          //宿舍固定资产编码
                              'dorm_property_description' => $dorm_property_description,//宿舍固定资产描述
                              'totalNumber' => $totalNumber,                            //总数量
                              'price' => $price,                                        //物品单价
                              'compensation_price' => $compensation_price               //赔偿单价
                            );
                            $this->db->insert('dorm_fixedassets', $data);
                            $query = $this->db->get_where('dorm_fixedassets', array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $floorNow, 'dorm_property_number' => $dorm_property_number));
                        }
                        $return = $this->db->affected_rows();
                    }
                    if ($return) 
                    {
                      $return_su = array('res' => '40000', 'msg' => '房间固定资产新增成功', 'data' => $query->result());
                      $result = json_encode($return_su);
                    } 
                    else 
                    {
                        $return_fa = array('res' => '40001', 'msg' => '固定资产新增失败', 'data' => '');
                        $result = json_encode($return_fa);
                    }
                //若楼层上限大于下限则报错
                }
                else
                {
                    $return = array('res' => '40000', 'msg' => '上限比下限大' ,'data' => '');
                    $result = json_encode($return);
                }
            }
            return $result;
        }

        /**
         * @description: 领用物品新增方法
         * @param {area_number, floor_number, article_name, article_number, price,compensation_price, totalNumber, remarks}
         * @return {*}
         */        
        public function receiving_items_add($area_number, $floor_number, $article_name, $article_number, $price, $compensation_price, $totalNumber, $remarks)
        {
            if ($area_number == '' || $article_name == '' || $article_number == '' || $price == '' || $compensation_price == '' || $totalNumber  == '' || $remarks == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                $data = array(
                    'area_number' => $area_number,              //区域编号
                    'floor_number' => $floor_number,            //宿舍楼编号
                    'article_name' => $article_name,            //物品名称
                    'article_number' => $article_number,        //物品编号
                    'price' => $price,                          //物品单价
                    'compensation_price' => $compensation_price,//赔偿价格
                    'totalNumber' => $totalNumber,              //总数量
                    'remarks' => $remarks                       //备注
                );
                $this->db->insert('dorm_receiving' ,$data);
                $return = $this->db->affected_rows();
                $query = $this->db->get_where('dorm_receiving', array('area_number' => $area_number, 'floor_number' => $floor_number, 'article_number' => $article_number));
                if ($return) 
                {
                    $return_su = array('res' => '40000', 'msg' => '领用物品新增成功', 'data' => $query->row());
                    $result = json_encode($return_su);
                } 
                else 
                {
                    $return_fa = array('res' => '40001', 'msg' => '领用物品新增失败', 'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
          }

        /**
         * @description: 领用物品停用方法
         * @param {area_number, floor_number, article_number}
         * @return {*}
         */        
        public function receiving_items_delete($area_number, $floor_number, $article_number)
        {
            if ($area_number == '' || $floor_number == '' || $article_number == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                //根据区域编号，宿舍楼编号，物品编号
                $where = array('area_number' => $area_number, 'floor_number' => $floor_number, 'article_number' => $article_number);
                $this->db->where($where);
                $this->db->delete('dorm_receiving', $where);
                $return = $this->db->affected_rows();
                if ($return) 
                {
                    $return_su = array('res' => '40000', 'msg' => '领用物品停用成功', 'data' => '');
                    $result = json_encode($return_su);
                } 
                else 
                {
                    $return_fa = array('res' => '40001', 'msg' => '领用物品停用失败', 'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        /**
         * @description: 领用物品修改方法
         * @param {area_number, floor_number, article_number, article_name, price, compensation_price, totalNumber, remarks}
         * @return {*}
         */        
        public function receiving_items_update($area_number, $floor_number, $article_number, $article_name, $price, $compensation_price, $totalNumber, $remarks)
        {
            if ($area_number == '' || $floor_number == '' || $article_number == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                $data = array(
                    'article_name' => $article_name,            //物品名称
                    'price' => $price,                          //物品单价
                    'compensation_price' => $compensation_price,//赔偿价格
                    'totalNumber' => $totalNumber,              //物品总数量
                    'remarks' => $remarks                       //备注
                );  
                $where = array('area_number' => $area_number, 'floor_number' => $floor_number, 'article_number' => $article_number);
                $this->db->where($where);
                $this->db->update('dorm_receiving', $data);
                $return = $this->db->affected_rows();
                $query = $this->db->get_where('dorm_receiving', $where);
                if ($return) 
                {
                    $return_su = array('res' => '40000', 'msg' => '领用物品修改成功', 'data' => $query->row());
                    $result = json_encode($return_su);
                } 
                else 
                {
                    $return_fa = array('res' => '40001', 'msg' => '领用物品修改失败', 'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }
        
        /**
         * @description: 领用物品查询方法
         * @param {area_number, floor_number, article_number, article_name, limit}
         * @return {*}
         */        
        public function receiving_items_select($area_number, $floor_number, $article_number, $article_name, $limit)
        {
            if ($limit == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '' );
                $result = json_encode($return);
            } 
            else 
            {
                //拼接sql
                $page = ("$limit" - 1) * 10;
                $sj1 = $area_number == '' ? ' 1 = 1 ' : " dorm_receiving.area_number = '" .$area_number. "'";
                $sj2 = $floor_number == '' ? ' AND 1 = 1 ' : " AND dorm_receiving.floor_number = '".$floor_number."'";
                $sj3 = $article_number == '' ? ' AND 1 = 1 ' : " AND article_number =  '".$Rarticle_number."'";
                $sj4 = $article_name == '' ? ' AND 1 =1 ' : " AND article_name LIKE '%".$article_name."%'";
                $sj5 = $limit == '' ? ' AND 1 = 1 ' : "limit " . $page . ",10";
                $sql = "SELECT * FROM dorm_receiving LEFT OUTER JOIN dorm_sslmanage on dorm_sslmanage.floor_number = dorm_receiving.floor_number WHERE" .$sj1 .$sj2 .$sj3 .$sj4 .$sj5;
                $query = $this->db->query($sql, $sj1, $sj2, $sj3, $sj4, $sj5);
                if ($query->num_rows() > 0) 
                {
                    $return_su = array('res' => '40000', 'msg' => '领用物品查询成功', 'data' => $query->result());
                    $result = json_encode($return_su);
                } 
                else 
                {
                    $return_fa = array('res' => '40001', 'msg' => '领用物品查询无数据', 'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        /**
         * @description: 房间物品新增方法_搜索方法：查询区域下是否有宿舍楼
         * @param {area_number}
         * @return {*}
         */        
        public function fixedassets_add_ssl_number($area_number)
        {
            if ($area_number == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                $this->db->select('floor_number');
                $this->db->where('area_number', $area_number);
                $query = $this->db->get('dorm_sslmanage');
                if ($query->num_rows() > 0) 
                {
                    $return_su = array('res' => '40000', 'msg' => '宿舍楼查询成功', 'data' => $query->result());
                    $result = json_encode($return_su);
                } 
                else 
                {
                    $return_fa = array('res' => '40001', 'msg' => '宿舍楼查询无数据', 'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        /**
         * @description: 房间物品新增方法_搜索方法：查询宿舍楼下楼层
         * @param {area_number, floor_number}
         * @return {*}
         */        
        public function fixedassets_add_ssl_floor($area_number, $floor_number)
        {
            if ($area_number == '' || $floor_number == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                $where = array('area_number' => $area_number, 'floor_number' => $floor_number);
                $this->db->select('floor');
                $this->db->where($where);
                $query = $this->db->get('dorm_sslmanage');
                if ($query->num_rows() > 0) 
                {
                    $return_su = array('res' => '40000', 'msg' => '宿舍楼层号查询成功', 'data' => $query->result());
                    $result = json_encode($return_su);
                } 
                else 
                {
                    $return_fa = array('res' => '40001', 'msg' => '宿舍楼层号查询无数据', 'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        /**
         * @description: 房间物品新增方法_搜索方法：查询宿舍楼楼层房间
         * @param {area_number, floor_number, dorm_floor}
         * @return {*}
         */        
        public function fixedassets_add_ss_number($area_number, $floor_number, $dorm_floor)
        {
            if ($area_number == '' || $floor_number == '' || $dorm_floor == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                $where = array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $dorm_floor);
                $this->db->select('room_number');
                $this->db->where($where);
                $query = $this->db->get('dorm_roominfor');
                if ($query->num_rows() > 0) 
                {
                    $return_su = array('res' => '40000', 'msg' => '宿舍楼房间号查询成功', 'data' => $query->result());
                    $result = json_encode($return_su);
                } 
                else 
                {
                    $return_fa = array('res' => '40001', 'msg' => '宿舍楼层号查询无数据', 'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        /**
         * @description: 房间物品新增方法
         * @param {area_number, floor_number, dorm_floor, room_number, dorm_property_description, dorm_property_number, price,compensation_price, totalNumber}
         * @return {*}
         */        
        public function fixedassets_add($area_number, $floor_number, $dorm_floor, $room_number, $dorm_property_description, $dorm_property_number, $price, $compensation_price, $totalNumber)
        {
            if ($area_number == '' || $floor_number == '' || $dorm_floor == '' || $room_number == '' || $dorm_property_description == '' || $dorm_property_number == '' || $price == '' || $compensation_price == '' || $totalNumber == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                $query = $this->db->get_where('dorm_fixedassets', array('area_number' => $area_number, 'floor_number' => $floor_number, 'dorm_floor' => $dorm_floor, 'room_number' => $room_number, 'dorm_property_number' => $dorm_property_number));
                //若房间没有这个固定资产则增加
                if ($query->num_rows() == '') 
                {
                    $data = array(
                        'area_number' => $area_number,                              //区域编号
                        'floor_number' => $floor_number,                            //宿舍楼编号
                        'dorm_floor' => $dorm_floor,                                //宿舍楼层号
                        'room_number' => $room_number,                              //宿舍房间号
                        'dorm_property_description' => $dorm_property_description,  //宿舍固定资产描述
                        'dorm_property_number' => $dorm_property_number,            //宿舍固定资产编码
                        'price' => $price,                                          //物品单价
                        'compensation_price' => $compensation_price,                //赔偿单价
                        'totalNumber' => $totalNumber                               //总数量
                        );
                    $this->db->insert('dorm_fixedassets' ,$data);
                    $return = $this->db->affected_rows();
                    //若有则将这个固定资产的总数量+1
                } 
                else 
                {
                    $where = array('area_number' => $area_number, 'dorm_floor' => $dorm_floor, 'room_number' => $room_number, 'dorm_property_number' => $dorm_property_number);
                    $this->db->select('totalNumber');
                    $this->db->where($where);
                    $query = $this->db->get('dorm_fixedassets');
                    $totalNumber = $query->row()->totalNumber + 1;
                    $data = array(
                        'totalNumber' => $totalNumber
                    );
                    $this->db->where($where);
                    $query_fixedassets = $this->db->update('dorm_fixedassets', $data);
                }

                $return = $this->db->get_where('dorm_fixedassets', array('area_number' => $area_number, 'floor_number' => $floor_number, 'room_number' => $room_number));
                if ($query_fixedassets) 
                {
                    $return_su = array('res' => '40000', 'msg' => '领用物品新增成功', 'data' => $return->row());
                    $result = json_encode($return_su);
                } 
                else 
                {
                    $return_fa = array('res' => '40001', 'msg' => '领用物品新增失败', 'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        /**
         * @description: 房间物品删除方法
         * @param {area_number, floor_number, dorm_floor, room_number, dorm_property_number}
         * @return {*}
         */        
        public function fixedassets_delete($area_number, $floor_number, $dorm_floor, $room_number, $dorm_property_number)
        {
            if ($area_number == '' || $floor_number == '' || $room_number == '' || $dorm_property_number == '') 
            {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                $where = array('area_number' => $area_number, 'floor_number' => $floor_number, 'room_number' => $room_number, 'dorm_floor' => $dorm_floor, 'dorm_property_number' => $dorm_property_number);
                $this->db->delete('dorm_fixedassets', $where);
                $query = $this->db->affected_rows();
                if ($query) 
                {
                    $return_su = array('res' => '40000', 'msg' => '房间物品删除成功', 'data' => '');
                    $result = json_encode($return_su);
                } 
                else 
                {
                    $return_fa = array('res' => '40001', 'msg' => '房间物品删除失败', 'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        /**
         * @description: 房间物品修改方法
         * @param {area_number, floor_number, room_number, dorm_property_number, dorm_property_description, price,compensation_price, dorm_property_remarks}
         * @return {*}
         */        
        public function fixedassets_update($area_number, $floor_number, $room_number, $dorm_property_number, $dorm_property_description, $price, $compensation_price, $dorm_property_remarks)
        {
            if ($area_number == '' || $floor_number == '' || $room_number == '' || $dorm_property_number == '') {
                $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($return);
            } 
            else 
            {
                $data = array(
                    'dorm_property_description' => $dorm_property_description,  //宿舍固定资产描述
                    'price' => $price,                                          //物品单价
                    'compensation_price' => $compensation_price,                //赔偿单价
                    'dorm_property_remarks' => $dorm_property_remarks,          //宿舍固定资产备注
                );
                $where = array('area_number' => $area_number, 'floor_number' => $floor_number, 'room_number' => $room_number, 'dorm_property_number' => $dorm_property_number);
                $this->db->where($where);
                $this->db->update('dorm_fixedassets', $data);
                $return = $this->db->affected_rows();
                $query = $this->db->get_where('dorm_fixedassets', $where);
                if ($return) 
                {
                    $return_su = array('res' => '40000', 'msg' => '房间物品修改成功', 'data' => $query->row());
                    $result = json_encode($return_su);
                } 
                else 
                {
                    $return_fa = array('res' => '40001', 'msg' => '房间物品修改失败', 'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }

        /**
         * @description: 房间物品查詢方法
         * @param {area_number, floor_number, dorm_floor, room_number, limit}
         * @return {*}
         */        
        public function fixedassets_select($area_number, $floor_number, $dorm_floor, $room_number, $limit)
        {
            //拼接sql
            $page = ("$limit" - 1) * 10;
            $sj1 = $area_number == '' ? ' 1 = 1 ' : " dorm_fixedassets.area_number = '".$area_number."'";
            $sj2 = $floor_number == '' ? ' AND 1 = 1 ' : " AND dorm_fixedassets.floor_number = '".$floor_number."'";
            $sj3 = $dorm_floor == '' ? ' AND 1 = 1 ' : " AND dorm_floor=  '".$dorm_floor."'";
            $sj4 = $room_number == '' ? ' AND 1 =1 ' : " AND room_number LIKE '%".$room_number."%'";
            $sj5 = $limit == '' ? ' AND 1 = 1 ' : "limit " . $page . ",10";
            $sql = "SELECT * FROM dorm_fixedassets LEFT OUTER JOIN dorm_sslmanage on dorm_sslmanage.floor_number = dorm_fixedassets.floor_number WHERE" .$sj1 .$sj2 .$sj3 .$sj4 .$sj5;
            $query = $this->db->query($sql, $sj1, $sj2, $sj3, $sj4, $sj5);
            if ($query->num_rows() > 0) 
            {
                $return_su = array('res' => '40000', 'msg' => '房间物品查询成功', 'data' => $query->result());
                $result = json_encode($return_su);
            } 
            else 
            {
                $return_fa = array('res' => '40001', 'msg' => '房间物品查询无数据', 'data' => '');
                $result = json_encode($return_fa);
            }
            return $result;
        }
    }
	